# 编译器设置
CC := gcc
CFLAGS := -Wall -Wextra -O2 -Iinclude -Iinclude/mbedtls
LDFLAGS := -lws2_32 -lbcrypt

# 目录结构设置
BUILD_DIR := build
SRC_DIR := .
LIB_DIR := library

# 源文件和目标文件
SRCS := $(wildcard $(SRC_DIR)/*.c) $(wildcard $(LIB_DIR)/*.c)
OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(filter $(SRC_DIR)/%,$(SRCS))) \
        $(patsubst $(LIB_DIR)/%.c,$(BUILD_DIR)/%.o,$(filter $(LIB_DIR)/%,$(SRCS)))
TARGET := mbedtls_app.exe  # 直接在当前目录生成

# 默认目标：仅编译
all: $(TARGET)
	@echo.
	@echo "Compilation complete"
	@echo "Executable file:$(TARGET)"

# 链接生成可执行文件 - 直接放在当前目录
$(TARGET): $(OBJS)
	@echo "Link to generate executable..."
	$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)

# 编译规则：自动创建目录
$(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
	@echo "compile $<..."
	$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%.o: $(LIB_DIR)/%.c | $(BUILD_DIR)
	@echo "compile $<..."
	$(CC) $(CFLAGS) -c $< -o $@

# 创建构建目录
$(BUILD_DIR):
	@mkdir -p $@

# 清理构建产物
clean:
	@rm -rf $(BUILD_DIR)  # 只删除build目录，保留.exe文件
	@rm -f $(TARGET)  # 同时删除当前目录的.exe文件
	@echo "Cleanup Complete - The build directory and the build directory have been removed $(TARGET)"

.PHONY: all clean